package com.opgea.crm.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opgea.crm.common.SetupsAttributeQualifier;
import com.opgea.crm.common.SetupsModuleQualifier;
import com.opgea.crm.common.vo.IssueCommentVo;
import com.opgea.crm.domain.entities.Issue;
import com.opgea.crm.domain.entities.IssueComment;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.modal.JsonModelMap;
import com.opgea.crm.services.EmployeeService;
import com.opgea.crm.services.IssueService;
import com.opgea.crm.services.ProjectDetailsService;
import com.opgea.crm.services.QualifierService;
import com.opgea.crm.services.SetupService;
import com.opgea.crm.web.form.IssueForm;

@Controller
@RequestMapping(value="issueComment")
public class IssueCommentController {
    Logger logger= Logger.getLogger(IssueCommentController.class);
    
    @Autowired
	SetupService setupService;
	@Autowired
	QualifierService qualifierService;
	@Autowired
	EmployeeService employeeService;
	@Autowired
	ProjectDetailsService projectDetailsService;
	@Autowired 
	IssueService issueService;
	
	@RequestMapping(method = RequestMethod.GET) 
	public String show(){
		logger.debug("I m in Show");
		return "issueComment";
	}

	// Method to add the issue
	@RequestMapping(value="save", method=RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> save(@RequestBody IssueForm form,
							 @RequestParam(required=true, value="issueId")String issueId) 
	{
	  
	   System.out.println("Issue CommentController >> save >> Inside Method");
	   Issue issue = issueService.find(new Long(issueId));
	   System.out.println("Issue Id : "+issueId);
	   
	   //comment related fields
	   List<IssueComment> comments = issue.getComments();
	   if(comments == null){
		   comments = new ArrayList<IssueComment>();
	   } 
	   IssueComment issueComment = new IssueComment();
	   issueComment.setIssue(issue);
	   issueComment.setComment(form.getComment()); 
	   issueComment.setCreatedAt(new Date()); 
	   issueComment.setStatus(setupService.findById(new Long(form.getIssueStatusId()))); 
	   issueComment.setAssignedTo(employeeService.find(new Long(form.getAssignedToId())));
	   comments.add(issueComment);
	   issue.setComments(comments);
	   issue.setStatus(setupService.findById(new Long(form.getIssueStatusId())));
	   issue.setAssignedTo(employeeService.find(new Long(form.getAssignedToId()))); 
	   issue.setResolution(setupService.findById(new Long(form.getResolutionId())));
	  
	   issueService.update(issue);
       return JsonModelMap.success().data(issue.getId());
	  }
	
	
	@RequestMapping(value ="/issueCommentReqCombo" ) 
	public @ResponseBody
	Map<String ,Object> issueCombo(@RequestParam(value ="operation") String operation,HttpServletResponse response)
	{
		List<SetupsValue> result = null;
		if(operation.equalsIgnoreCase("issueCategory"))
			result  = setupService.listSetupValue(SetupsModuleQualifier.Issue.ordinal(), SetupsAttributeQualifier.Category.ordinal());
		if(operation.equalsIgnoreCase("issueStatus"))
			result = setupService.listSetupValue(SetupsModuleQualifier.Issue.ordinal(), SetupsAttributeQualifier.Status.ordinal());
		if(operation.equalsIgnoreCase("issuePriority")) 
			result  = setupService.listSetupValue(SetupsModuleQualifier.Issue.ordinal(), SetupsAttributeQualifier.Priority.ordinal());
		if(operation.equalsIgnoreCase("issueComplexity"))  
			result  = setupService.listSetupValue(SetupsModuleQualifier.Issue.ordinal(), SetupsAttributeQualifier.Complexity.ordinal()); 
		if(operation.equalsIgnoreCase("issueResolution"))
			result =setupService.listSetupValue(SetupsModuleQualifier.Issue.ordinal(), SetupsAttributeQualifier.Resolution.ordinal());
		return JsonModelMap.success().data(result);
	}
	
	
	@RequestMapping(method=RequestMethod.GET, value="issueAssignedTo")
    public @ResponseBody Map<String, Object> issueAssignedTo(@RequestParam(required=false) Long projectId) 
    {
	   List<SetupsValue> issueAssignedTo = null;
	   if(projectId!=null)  
	   {
		  
		   issueAssignedTo = issueService.allProjectAndMembers(projectId); 
	   } 
 	   return JsonModelMap.success().data(issueAssignedTo);
    }
	
	@RequestMapping(method=RequestMethod.GET , value ="showIssueCommentDetailsInPopup")
	public @ResponseBody
	Map <String , Object> showIssueDetailsInPopup(@RequestParam("issueId") String issueId)
	{
		Issue issue = issueService.find(new Long(issueId));
		return JsonModelMap.success().data(new IssueForm(issue)); 
	} 
	@RequestMapping(method=RequestMethod.GET, value="showComments")
	public @ResponseBody Map<String, Object> commentList(@RequestParam(required=false) String issueId){
		List<IssueCommentVo> comments = null;
	    if(issueId!= null)
		 comments = issueService.findAllCommentsByIssueId(new Long(issueId));  
		 return JsonModelMap.success().data(comments); 
	}  
} 
